﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Northwind.Interfaces.Data.Model;
using System.ComponentModel;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Northwind.ViewModel.Messeges;
using GalaSoft.MvvmLight;
using Northwind.Application.Model;

namespace Northwind.ViewModel
{
    public class OrderViewModel : ViewModelBase, IOrderModel
    {
        #region Fields

        IOrderModel _model;

        #endregion

        #region Constructors

        public OrderViewModel(IOrderModel model)
        {
            _model = model;

            ((INotifyPropertyChanged)_model).PropertyChanged += new PropertyChangedEventHandler(OrderModel_PropertyChanged);
        }

        #endregion

        #region IOrderModel Implementation

        public int OrderID
        {
            get { return _model.OrderID; }
        }

        public decimal Total
        {
            get { return _model.Total; }
            private set
            {
                RaisePropertyChanged("Total");
            }
        }

        public ICustomerModel Customer
        {
            get
            {
                return new CustomerViewModel(_model.Customer);
            }
            set
            {
                _model.Customer = value;
                RaisePropertyChanged("Customer");
            }
        }

        public IEmployeeModel Employee
        {
            get
            {
                return _model.Employee;
            }
            set
            {
                _model.Employee = value;
                RaisePropertyChanged("Employee");
            }
        }

        public System.Collections.ObjectModel.ObservableCollection<IOrderDetailModel> OrderDetails
        {
            get { return _model.OrderDetails; }
        }

        //int OrderID
        //{
        //    get
        //    {
        //        return _model.OrderID;
        //    }
        //    set
        //    {
        //        //_model.OrderID = value;
        //        RaisePropertyChanged("OrderID");
        //    }
        //}

        public string CustomerID
        {
            get
            {
                return _model.CustomerID;
            }
            set
            {
                _model.CustomerID = value;
                RaisePropertyChanged("CustomerID");
            }
        }

        public int? EmployeeID
        {
            get
            {
                if (this.Employee != null)
                {
                    return this.Employee.EmployeeID;
                }
                return null;
            }
            //set
            //{

            //}
        }

        public DateTime? OrderDate
        {
            get
            {
                return _model.OrderDate;
            }
            set
            {
                _model.OrderDate = value;
                RaisePropertyChanged("OrderDate");
            }
        }

        public DateTime? ShippedDate
        {
            get
            {
                return _model.ShippedDate;
            }
            set
            {
                _model.ShippedDate = value;
                RaisePropertyChanged("ShippedDate");
            }
        }

        public decimal? Freight
        {
            get
            {
                return _model.Freight;
            }
            set
            {
                _model.Freight = value;
                RaisePropertyChanged("Freight");
            }
        }

        #endregion

        #region Events

        // public event PropertyChangedEventHandler PropertyChanged = delegate { };

        #endregion

        #region Helpers

        void OrderModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            //PropertyChanged(sender, e);
            RaisePropertyChanged(e.PropertyName);
        }

        #endregion

        int Interfaces.Data.IOrder.OrderID
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        int? Interfaces.Data.IOrder.EmployeeID
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
}
